﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bigeagle.Portable.BoardGames;
using Bigeagle.Portable.BoardGames.Go;
using Windows.Storage;
using System.IO;
using Windows.Storage.Streams;
using System.Diagnostics;
namespace EagleGO
{
    class KiFUCategory
    {
        string _Name;

        public string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
        List<KiFUItem> _Items = new List<KiFUItem>();

        public KiFUCategory(string name)
        {
            // TODO: Complete member initialization
            _Name = name;
        }

        public List<KiFUItem> Items
        {
            get { return _Items; }
        }
    }//end class

        /// <summary>
    /// 棋谱项，用于首页列表显示的数据类
    /// </summary>
    class KiFUItem
    {
        /// <summary>
        /// 分类
        /// </summary>
        string _Category;

        /// <summary>
        /// 设置或获得分类
        /// </summary>
        public string Category
        {
            get 
            { 
                return _Category; 
            }
            set
            {
                _Category = value;
            }
        }

        /// <summary>
        /// 棋局信息
        /// </summary>
        GoGameInfo _GameInfo;

        /// <summary>
        /// 棋局信息
        /// </summary>
        [System.Xml.Serialization.XmlIgnore]
        public GoGameInfo GameInfo
        {
            get 
            { 
                return _GameInfo; 
            }
            set 
            { 
                _GameInfo = value; 
            }
        }

        /// <summary>
        /// 棋谱的uri
        /// </summary>
        Uri _KiFUUri;

        /// <summary>
        /// 设置或获得棋谱uri
        /// </summary>
        public Uri KiFUUri
        {
            get 
            { 
                return _KiFUUri; 
            }
            set 
            {
                    _KiFUUri = value;
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public KiFUItem()
        {
            _Category = string.Empty;

        }//end method

        /// <summary>
        /// 改写构造函数
        /// </summary>
        /// <param name="category">分类</param>
        /// <param name="gameInfo">棋局信息</param>
        /// <param name="kifuUri">棋谱uri</param>
        public KiFUItem(string category,  Uri kifuUri)
        {
            _Category = category;
            _KiFUUri = kifuUri;
        }

        /// <summary>
        /// 读取棋谱信息
        /// </summary>
        /// <param name="encoding"></param>
        public async Task<bool> LoadGameInfoAsync(Encoding encoding)
        {
            try
            {
                if (_KiFUUri != null)
                {
                    SGFKiFUSerializer serializer = new SGFKiFUSerializer(encoding);
                    if (_KiFUUri.IsFile)
                    {
                        StorageFile file = await StorageFile.GetFileFromPathAsync(KiFUUri.LocalPath);
                        using (Stream stream = await file.OpenStreamForReadAsync())
                        {
                            _GameInfo = serializer.ReadGameInfo(stream);
                        }

                        return true;
                    }
                    else
                    {
                        var streamRef = RandomAccessStreamReference.CreateFromUri(_KiFUUri);
                        using (IRandomAccessStreamWithContentType streamRandom = await streamRef.OpenReadAsync())
                        {
                            _GameInfo = serializer.ReadGameInfo(streamRandom.AsStream((int)streamRandom.Size));
                            Debug.WriteLine("{0}({1}-{2})", _GameInfo.Event
                                , _GameInfo.BlackPlayer.Name, _GameInfo.WhitePlayer.Name);

                        }
                        return true;
                    }
                }
            }
            catch (Exception)
            {
                return false;
            }
            return false;
        }//end method

        public async Task<bool> LoadGameInfoAsync()
        {
           return await LoadGameInfoAsync(Encoding.UTF8);
        }

        /// <summary>
        /// 载入棋局
        /// </summary>
        /// <param name="encoding"></param>
        /// <returns></returns>
        public async Task<KiFUGame> LoadKiFUGameAsync(Encoding encoding)
        {
            KiFUGame result = null ;
            if (_KiFUUri != null)
            {
                SGFKiFUSerializer serializer = new SGFKiFUSerializer(encoding);
                if (_KiFUUri.IsFile)
                {
                    StorageFile file = await StorageFile.GetFileFromPathAsync(KiFUUri.LocalPath);
                    using (Stream stream = await file.OpenStreamForReadAsync())
                    {
                        byte[] buffer = new byte[stream.Length] ;
                        int i = await stream.ReadAsync(buffer, 0, buffer.Length);
                        string content = encoding.GetString(buffer, 0, buffer.Length);
                        result = serializer.DeSerialize(content);
                    }

                }
                else
                {
                    var streamRef = RandomAccessStreamReference.CreateFromUri(_KiFUUri);
                    using (IRandomAccessStreamWithContentType streamRandom = await streamRef.OpenReadAsync())
                    {
                        using (Stream stream = streamRandom.AsStream((int)streamRandom.Size))
                        {
                            byte[] buffer = new byte[stream.Length];
                            int i = await stream.ReadAsync(buffer, 0, buffer.Length);
                            string content = encoding.GetString(buffer, 0, buffer.Length);
                            result = serializer.DeSerialize(content);
                        }
                    }
                }
            }
            return result;

        }//end method
    }//end class

}//end namespace
